﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OF.Common;
namespace OF.Web.admin
{
    public partial class register : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string name = txtloginname.Text.Trim();
            string pwd = txtpwd.Text.Trim();
            string pwd2 = txtpwd2.Text.Trim();
            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(pwd2))
            {
                tip.InnerText = "帐号或密码不能为空";
                tip.Style.Add(HtmlTextWriterStyle.Display, "block");
                return;
            }
            if (!Common.PageValidate.IsPhone(name))
            {
                tip.InnerText = "帐号必须是手机号";
                tip.Style.Add(HtmlTextWriterStyle.Display, "block");
                return;
            }
            if (pwd != pwd2)
            {
                tip.InnerText = "密码和确认密码不一致";
                tip.Style.Add(HtmlTextWriterStyle.Display, "block");
                return;
            }

            OF.BLL.User.Seller bll_seller = new BLL.User.Seller();
            var ds = bll_seller.GetList("loginname='" + txtloginname.Text + "' and state !=2");
            if (ds != null && ds.Tables[0] != null && ds.Tables[0].Rows.Count > 0)
            {
                tip.InnerText = "帐号已经被使用过";
                tip.Style.Add(HtmlTextWriterStyle.Display, "block");
                return;
            }

            Model.Seller model = new Model.Seller();
            model.loginname = txtloginname.Text;
            model.pwd = txtpwd.Text;
            model.state = Convert.ToInt32(Enmus.USER_STATE.INIT);
            model.order_type = 0;
            int newid = bll_seller.Add(model);
            if (newid > 0)
            {
                WebUtils.AddSession(txtloginname.Text, newid.ToString());
                MessageBox.ShowAndRedirect(this, "注册成功", "index.aspx");
            }
            else
            {
                tip.InnerText = "注册失败,请稍后重试";
                tip.Style.Add(HtmlTextWriterStyle.Display, "block");
                return;
            }
        }
    }
}